<template>
	<view class="page" :style="loading ? 'opacity: 0;' : 'opacity: 1;'">
		<pageTxt>
			<z-paging ref="paging" :paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }">
				<!-- 头部 -->
				<template slot="top">
					<view class="navbar-box">
						<u-navbar title="发起优惠审批" :autoBack="true" :fixed="false"></u-navbar>
					</view>
				</template>
				<view class="add-content ios">
					<u--form labelPosition="top" :model="addDiscountRuleForm" ref="uForm" labelWidth="auto">
						<view class="form-box">
							<u-form-item labelPosition="top" labelWidth="100%" required label="审批类型" borderBottom>
								<u-radio-group v-model="addDiscountRuleForm.type" placement="row" @change="radioChaneFunc">
									<u-radio label="签约优惠" :name="1"></u-radio>
									<u-radio label="结算优惠" :name="2"></u-radio>
								</u-radio-group>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="项目名称" borderBottom @click="cascaderFunc('projectShow')">
								<u--input
									placeholder="选择项目"
									inputAlign="left"
									v-model="addDiscountRuleForm.project_name"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIcon="arrow-right"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required :label="addDiscountRuleForm.type == 1 ? '预算报价金额' : '结算报价金额'" borderBottom>
								<u--input
									:placeholder="addDiscountRuleForm.type == 1 ? '预算报价金额' : '结算报价金额'"
									inputAlign="left"
									v-model="addDiscountRuleForm.budget_quotation_amount"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">元</view>
								</template>
							</u-form-item>
							<u-form-item
								labelPosition="top"
								labelWidth="100%"
								required
								:label="addDiscountRuleForm.type == 1 ? '预计签约金额' : '预计结算金额'"
								borderBottom
								v-if="addDiscountRuleForm.project_id"
							>
								<u--input
									@input="inputFunc($event, 'expected_signing_amount')"
									@change="JSpriceFunc"
									type="number"
									:placeholder="`输入${addDiscountRuleForm.type == 1 ? '预计签约金额' : '预计结算金额'}`"
									inputAlign="left"
									v-model="addDiscountRuleForm.expected_signing_amount"
									border="none"
									color="#000000"
									fontSize="28rpx"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">元</view>
								</template>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="成本金额" borderBottom>
								<u--input
									@input="inputFunc($event, 'cost_amount')"
									@change="JSpriceFunc"
									placeholder="成本金额"
									inputAlign="left"
									v-model="addDiscountRuleForm.cost_amount"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">元</view>
								</template>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="优惠金额" borderBottom>
								<u--input
									placeholder="优惠金额"
									inputAlign="left"
									v-model="addDiscountRuleForm.discount_amount"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">元</view>
								</template>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="优惠后利润金额" borderBottom>
								<u--input
									placeholder="优惠后利润金额"
									inputAlign="left"
									v-model="addDiscountRuleForm.remaining_profit_amount"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">元</view>
								</template>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="优惠后成本利润率" borderBottom>
								<u--input
									placeholder="优惠后成本利润率"
									inputAlign="left"
									v-model="addDiscountRuleForm.remaining_cost_profit_ratio"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">%</view>
								</template>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="优惠前利润金额" borderBottom>
								<u--input
									placeholder="优惠前利润金额"
									inputAlign="left"
									v-model="addDiscountRuleForm.profit_amount"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">元</view>
								</template>
							</u-form-item>
							<u-form-item labelPosition="top" labelWidth="100%" required label="优惠前成本利润率" borderBottom>
								<u--input
									placeholder="优惠前成本利润率"
									inputAlign="left"
									v-model="addDiscountRuleForm.cost_profit_ratio"
									border="none"
									color="#000000"
									fontSize="28rpx"
									disabled
									disabledColor="#ffffff"
									suffixIconStyle="color: #727272;font-size:24rpx;font-weight:500;"
									placeholderStyle="font-weight: 400;font-size: 28rpx;color: #B1B1B0;"
								></u--input>
								<template v-slot:right>
									<view class="unit">%</view>
								</template>
							</u-form-item>
						</view>
					</u--form>
				</view>
				<view class="footer ios">
					<u-button
						text="提交"
						@click="submit"
						customStyle="width: 702rpx;height: 88rpx;background: #3B75FF;border-radius: 16rpx;font-weight: 500;font-size: 28rpx;color: #FFFFFF;margin:20rpx 24rpx;"
					></u-button>
				</view>
			</z-paging>
			<!-- 项目列表 -->
			<u-picker
				title="项目列表"
				closeOnClickOverlay
				:show="projectShow"
				:columns="[projectList]"
				@confirm="confirmFunc($event, 'projectShow')"
				@cancel="closeFunc('projectShow')"
				@close="closeFunc('projectShow')"
				keyName="project_name"
			></u-picker>
		</pageTxt>
	</view>
</template>

<script>
export default {
	data() {
		return {
			loading: false,
			list: [],
			addDiscountRuleForm: {
				project_id: '',
				project_name: '',
				type: 1,
				budget_quotation_amount: '',
				expected_signing_amount: '',
				discount_amount: '',
				remaining_profit_amount: '',
				remaining_cost_profit_ratio: '',
				profit_amount: '',
				cost_profit_ratio: '',
				cost_amount: ''
			},
			projectList: [], //项目列表
			projectShow: false //项目弹窗
		};
	},
	onLoad(e) {
		this.selectProjectList(
			{
				less_status: [1105],
				greater_status: [1092]
			},
			e
		);
	},
	methods: {
		inputFunc(e, type) {
			this.$nextTick(() => {
				this.addDiscountRuleForm[type] = this.$inputFunc(e);
			});
		},
		radioChaneFunc(type) {
			if (type == 1) {
				this.selectProjectList({
					less_status: [1105],
					greater_status: [1092]
				});
			} else {
				this.selectProjectList({
					greater_status: [1138]
				});
			}
			this.addDiscountRuleForm = {
				project_id: '',
				project_name: '',
				type: this.addDiscountRuleForm.type,
				budget_quotation_amount: '',
				expected_signing_amount: '',
				discount_amount: '',
				remaining_profit_amount: '',
				remaining_cost_profit_ratio: '',
				profit_amount: '',
				cost_profit_ratio: '',
				cost_amount: ''
			};
		},
		// picker 弹窗打开
		cascaderFunc(type) {
			switch (type) {
				// 项目
				case 'projectShow':
					this.projectShow = true;
					break;
				default:
			}
		},
		// picker 弹窗关闭
		closeFunc(type) {
			switch (type) {
				case 'projectShow':
					this.projectShow = false;
					break;
				default:
			}
		},
		// picker 弹窗选择
		confirmFunc(e, type) {
			console.log(e);
			switch (type) {
				case 'projectShow':
					this.addDiscountRuleForm.project_name = e.value[0].project_name;
					this.addDiscountRuleForm.project_id = e.value[0].id;
					this.selectProjectDetails(e.value[0].id);
					this.projectShow = false;
					break;
				default:
			}
		},
		// 项目列表
		selectProjectList(obj) {
			this.$Apipc.selectProjectList(obj).then((res) => {
				console.log(res, '项目列表');
				if (res.code == 200) {
					this.projectList = res.data;
				}
			});
		},
		afterRead(files) {
			files.map((item) => {
				let obj = {
					url: item.url
				};
				this.addDiscountRuleForm.site_file.push(obj);
				console.log(this.addDiscountRuleForm.site_file, 'afterRead');
			});
		},
		deletePic(e) {
			console.log(e, 'deletePic');
			this.addDiscountRuleForm.site_file.splice(e.index, 1);
		},
		submit() {
			let addDiscountRuleForm = this.addDiscountRuleForm;
			console.log(addDiscountRuleForm);
			if (!addDiscountRuleForm.project_name) {
				uni.showToast({
					title: '请选择项目',
					icon: 'error'
				});
				return;
			}
			if (addDiscountRuleForm.type == 1 && !addDiscountRuleForm.expected_signing_amount) {
				uni.showToast({
					title: '请输入预计报价金额',
					icon: 'error'
				});
				return;
			}
			if (addDiscountRuleForm.type == 2 && !addDiscountRuleForm.expected_signing_amount) {
				uni.showToast({
					title: '请输入预计结算金额',
					icon: 'error'
				});
				return;
			}
			this.addDiscount({
				project_id: addDiscountRuleForm.project_id,
				type: addDiscountRuleForm.type,
				budget_quotation_amount: addDiscountRuleForm.budget_quotation_amount,
				expected_signing_amount: addDiscountRuleForm.expected_signing_amount,
				discount_amount: addDiscountRuleForm.discount_amount,
				remaining_profit_amount: addDiscountRuleForm.remaining_profit_amount,
				remaining_cost_profit_ratio: addDiscountRuleForm.remaining_cost_profit_ratio,
				profit_amount: addDiscountRuleForm.profit_amount,
				cost_profit_ratio: addDiscountRuleForm.cost_profit_ratio,
				cost_amount: addDiscountRuleForm.cost_amount
			});
		},
		// 发起优惠审批
		addDiscount(data) {
			uni.showLoading({
				title: '信息提交中',
				mask: true
			});
			this.$Apipc.addDiscount(data).then((res) => {
				console.log(res, '发起优惠审批');
				uni.hideLoading();
				if (res.code == 200) {
					var pages = getCurrentPages();
					var perPage = pages[pages.length - 2];
					if (perPage && perPage.$vm.$refs.discountApproval) {
						perPage.$vm.$refs.discountApproval.searchFunc();
					}

					setTimeout(() => {
						var currentPages = getCurrentPages()[getCurrentPages().length - 2];
						if (currentPages) {
							uni.navigateBack({
								delta: 1
							});
						} else {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}
					}, 500);
				}
				uni.showToast({
					title: res.message,
					icon: res.code == 200 ? 'success' : 'error'
				});
			});
		},
		// 项目详情
		selectProjectDetails(project_id) {
			this.$Apipc
				.selectProjectDetails({
					project_id
				})
				.then((res) => {
					console.log(res, '项目详情');
					if (res.code == 200) {
						this.projectDetail = res.data;
						if (this.addDiscountRuleForm.type == 1) {
							this.addDiscountRuleForm.budget_quotation_amount = res.data.system_quotation; //预算报价金额
							this.addDiscountRuleForm.expected_signing_amount = res.data.quotation_amount || ''; //预算签约金额
							this.addDiscountRuleForm.discount_amount = res.data.discount_amount; //优惠金额
							this.addDiscountRuleForm.profit_amount = res.data.quotation_profit_amount; //优惠前利润金额
						} else {
							if (!res.data.settlement_cost_amount || res.data.settlement_cost_amount == '0') {
								uni.showToast({
									icon: 'error',
									title: '该项目未结算，无法发起优惠审批'
								});
								return;
							}
							this.addDiscountRuleForm.budget_quotation_amount = res.data.settlement_amount; //预算报价金额
							// this.addDiscountRuleForm.expected_signing_amount = res.data.settlement_quotation_amount || ''; //预算签约金额
							// let discount_amount = Number(res.data.settlement_amount) - Number(res.data
							// 	.settlement_quotation_amount);
							// this.addDiscountRuleForm.discount_amount = discount_amount; //优惠金额
							this.addDiscountRuleForm.profit_amount = res.data.profit_amount; //优惠前利润金额
							this.addDiscountRuleForm.cost_profit_ratio = res.data.settlement_profit_margin; //优惠前利润率
						}

						// 计算金额
						this.mathFunc();
					}
				});
		}, // 金额计算
		mathFunc() {
			let projectDetail = this.projectDetail;
			let { discount_amount } = this.addDiscountRuleForm;
			// 签约优惠
			if (this.addDiscountRuleForm.type == 1) {
				//优惠前成本利润率（报价金额-成本金额）/成本 * 100%
				let cost_profit_ratio = '';
				cost_profit_ratio = Number(projectDetail.system_quotation) - Number(projectDetail.cost_amount);
				cost_profit_ratio = Number(cost_profit_ratio) / Number(projectDetail.cost_amount);
				cost_profit_ratio = Number(cost_profit_ratio * 100).toFixed(2);
				this.addDiscountRuleForm.cost_profit_ratio = cost_profit_ratio;

				// 优惠后利润金额计算（【利润金额】-【优惠金额】） ---弃用
				// 优惠审批页面【预计签约金额】-（优惠审批界面填写的成本金额+预算的现场采购成本+预算的公司集采成本）   ---现用  2025-9-19
				if (String(this.addDiscountRuleForm.expected_signing_amount) && String(this.addDiscountRuleForm.cost_amount)) {
					let remaining_profit_amount = '';
					remaining_profit_amount = Number(projectDetail.procurement_cost) + Number(projectDetail.centralized_procurement_cost);
					remaining_profit_amount = Number(remaining_profit_amount) + Number(this.addDiscountRuleForm.cost_amount);
					this.addDiscountRuleForm.remaining_profit_amount = (Number(this.addDiscountRuleForm.expected_signing_amount) - Number(remaining_profit_amount)).toFixed(2);
				}

				// 优惠后成本利润率（预算报价金额-预算成本金额-优惠金额）/成本金额*100% ---弃用
				// 【预算报价金额-（优惠审批界面填写的成本金额+预算的现场采购成本+预算的公司集采成本 + 报价税金）-优惠金额】/ 成本金额*100%    ---现用  2025-9-19
				if (String(projectDetail.procurement_cost) && String(projectDetail.centralized_procurement_cost) && String(this.addDiscountRuleForm.cost_amount)) {
					let remaining_cost_profit_ratio = '';
					let cost_amount = '';
					cost_amount = Number(projectDetail.procurement_cost) + Number(projectDetail.centralized_procurement_cost);
					cost_amount = Number(cost_amount) + Number(this.addDiscountRuleForm.cost_amount);
					cost_amount = Number(cost_amount) + Number(projectDetail.quotation_taxes);

					remaining_cost_profit_ratio = Number(this.addDiscountRuleForm.budget_quotation_amount) - Number(cost_amount);
					remaining_cost_profit_ratio = Number(remaining_cost_profit_ratio) - Number(discount_amount);
					remaining_cost_profit_ratio = Number(remaining_cost_profit_ratio) / Number(cost_amount);
					this.addDiscountRuleForm.remaining_cost_profit_ratio = (Number(remaining_cost_profit_ratio) * 100).toFixed(2);
				}
			} else {
				// 结算优惠

				// 优惠后利润金额计算
				// （【利润金额】-【优惠金额】）    ---弃用
				// 优惠审批页面【预计结算金额】-（优惠审批界面填写的成本金额+结算的现场采购成本+结算的公司集采成本）    ---现用  2025-9-19
				if (String(this.addDiscountRuleForm.cost_amount) && String(this.addDiscountRuleForm.expected_signing_amount)) {
					let remaining_profit_amount = '';
					remaining_profit_amount = Number(projectDetail.final_procurement_cost) + Number(projectDetail.final_centralized_procurement_cost);
					remaining_profit_amount = Number(remaining_profit_amount) + Number(this.addDiscountRuleForm.cost_amount);
					this.addDiscountRuleForm.remaining_profit_amount = Number(this.addDiscountRuleForm.expected_signing_amount) - Number(remaining_profit_amount);
				}

				// 优惠后成本利润率
				// （结算报价金额-结算成本金额-优惠金额）/结算成本金额*100%    ---弃用
				// 【结算报价金额-（优惠审批界面填写的成本金额+结算的现场采购成本+结算的公司集采成本）-优惠金额）/成本金额*100%     ---现用  2025-9-19
				if (String(this.addDiscountRuleForm.budget_quotation_amount) && String(this.addDiscountRuleForm.discount_amount) && String(this.addDiscountRuleForm.cost_amount)) {
					let remaining_cost_profit_ratio = '';
					let cost_amount = '';
					cost_amount = Number(projectDetail.final_procurement_cost) + Number(projectDetail.final_centralized_procurement_cost);
					cost_amount = Number(cost_amount) + Number(this.addDiscountRuleForm.cost_amount);
					remaining_cost_profit_ratio = Number(cost_amount) + Number(this.addDiscountRuleForm.discount_amount);
					remaining_cost_profit_ratio = Number(this.addDiscountRuleForm.budget_quotation_amount) - Number(remaining_cost_profit_ratio);
					remaining_cost_profit_ratio = Number(remaining_cost_profit_ratio) / Number(cost_amount);
					this.addDiscountRuleForm.remaining_cost_profit_ratio = (Number(remaining_cost_profit_ratio) * 100).toFixed(2);
				}
			}
		},
		// 输入预计签约金额后进行计算
		JSpriceFunc(val) {
			let { expected_signing_amount, budget_quotation_amount } = this.addDiscountRuleForm;
			if (val && typeof val === 'number' && !isNaN(val)) return;
			// 优惠金额 = 预算报价-预计签约金额
			let discount_amount = Number(budget_quotation_amount) - Number(expected_signing_amount);

			this.addDiscountRuleForm.discount_amount = discount_amount.toFixed(2);
			this.$nextTick(() => {
				this.mathFunc();
			});
		}
	}
};
</script>

<style lang="scss" scoped></style>
